$(function () {
  // 图标可视化图标
  var buildingId;



  //创建二维码
  function createQRCode(id, url, src) {
    $('#' + id).empty();
    jQuery('#' + id).qrcode({
      render: 'canvas',
      text: url,
      background: "#fff", //二维码的后景色  
      foreground: "#000", //二维码的前景色  
      size: 170,
      // quiet: 1 // 安静区域 即周边空白区域
      src: src, //图片中央的二维码
    });
  }

  $("body").on("click", ".inspectionDetails-target", function (event) {
    event.preventDefault();
    $(".inspectionDetails-pop").addClass("is-visible3");
    uuid = $(this).attr('uuid');
    onlyValue = $(this).attr('onlyValue');
    buildingId = $(this).attr("buildingId");
    console.log(buildingId);
    visualizationCanvasFunc();
    inspectionDetailsPageList(1, onlyValue, uuid);
    var buildingName = $(this).attr('buildingName');
    console.log(buildingName)
    $('#inspectionDetailsModal .erweimaBtn').attr('buildingName', buildingName);
    $('#inspectionDetailsModal .currSeeDetails span').attr('uuid', uuid);
    $('#inspectionDetailsModal .currSeeDetails span').attr('onlyValue', onlyValue);
    $('#inspectionDetailsModal .currSeeDetails span').attr('buildingName', buildingName);
    $('#inspectionDetailsModal .currSeeDetails span').attr('buildingId', buildingId);
    $('#inspectionDetailsModal .lastSeeDetails span').attr('uuid', uuid);
    $('#inspectionDetailsModal .lastSeeDetails span').attr('onlyValue', onlyValue);
    $('#inspectionDetailsModal .lastSeeDetails span').attr('buildingName', buildingName);
    $('#inspectionDetailsModal .lastSeeDetails span').attr('buildingId', buildingId);
  });
  $('#inspectionDetailsModal .seeDetails').on('click', function () {
    $(this).siblings('.seeDetails').hide();
  })
  // 楼宇巡检详情页面 关闭
  $(".inspectionDetails-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close") ||
      $(event.target).is(".inspectionDetails-pop")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
    }
  });
  // 点击楼宇巡检详情二维码按钮弹出二维码
  $('#inspectionDetailsModal .headListRight .erweimaBtn').on('click', function () {
    $('#inspectionDetailsQrCode').addClass('is-visible3');
    // 生成二维码
    var name = $(this).attr('buildingName');
    createQRCodeLabel('#inspectionDetailsQrCodeImg', 170, uuid, "img/logo.png", name);
    $('#inspectionDetailsQrCode .checkEarlyWarningDetailsQrCodeCode .uuid').text(uuid);
  });

  
  // 主要隐患 tip 显示全部信息
  $('#inspectionDetailsModal .inspectionDetailsTableBox').on("mouseover mouseout", '.majorHiddenDangersBox', function (event) {
    var $this = $(this);
    var $workingRange = $this.find('.majorHiddenDangers');
    var outerHeight = $workingRange.outerHeight();
    if (outerHeight > 30) {
      if (event.type == "mouseover") {
        //鼠标悬浮
        $workingRange.show();
        $workingRange.css('top', -outerHeight + 'px');

      } else if (event.type == "mouseout") {
        //鼠标离开
        $workingRange.hide();

      }
    }

  });
});
// 楼宇巡检详情 显示
// $(".inspectionDetails-pop").addClass("is-visible3");
var uuid = '';
var onlyValue = '';

function Percentage(num, total) {
  return (Math.round(num / total * 10000) / 100.00 + "%"); // 小数点后两位百分比
}


// --------------------------------

// 楼宇巡检详情页面 分页功能
var inspectionDetailsPageNum = 1;
var inspectionDetailsPageNumCurr = 1;

function patrollingPageFunc(pageSum, startPageNum) {
  console.log(pageSum);
  if (pageSum <= 1) {
    $('#inspectionDetailsPageBox .pageJump').hide()
  } else {
    $('#inspectionDetailsPageBox .pageJump').show()
  }
  Page({
    num: pageSum, //页码数
    startnum: startPageNum, //指定页码
    elem: $('#inspectionDetailsPage'), //指定的元素
    callback: function (n) { //回调函数
      inspectionDetailsPageNumCurr = n;
      // $('#earlyWarningPageBox .pageJump').show();
      inspectionDetailsPageList(n, onlyValue, uuid);
      // 当翻到下一页时 清除掉 全选按钮的 选中状态
      $("#inspectionDetailsSelectAll").prop("checked", false);
    }
  });
};

// 渲染 楼宇巡检详情页面 的列表
function inspectionDetailsPageList(n, onlyValue, uuid) {
  var obj = {};
  obj.uuid = uuid;
  obj.onlyValue = onlyValue;
  obj.size = 9;
  obj.page = n - 1;
  var ret = getBuildingInspectionRecordDetails(obj);
  ret.then(function (data) {
    console.log(data);

    // $('#inspectionDetailsModal .headListCenter').show();
    if (data.data.totalPages < 1) {
      $('#inspectionDetailsPageBox .pageJump').hide();
    } else {
      $('#inspectionDetailsPageBox .pageJump').show();
    }
    var content = data.data.content;
    inspectionDetailsPageNum = data.data.totalPages;
    size = data.data.size;
    $('#inspectionDetailsModal .tbodyList').html('');
    for (var i = 0; i < content.length; i++) {
      var level = content[i].level;
      // if (level == 0) {
      //   level = '重大';
      // } else if (level == 1) {
      //   level = '较大';
      // } else if (level == 2) {
      //   level = '一般';
      // } else if (level == 3) {
      //   level = '正常';
      // }
      var active='';
      if (level == 0) {
        level = '重大';
        active = 'colorff4242';
      } else if (level == 1) {
        level = '较大';
        active = 'colorffb142';
      } else if (level == 2) {
        level = '一般';
        active = 'color61ff42';
      } else if (level == 3) {
        level = '正常';
        active = 'color61ff42';
      }
      var duration = content[i].duration;
      var majorHiddenDangersStr = content[i].majorHiddenDangers;
      if (majorHiddenDangersStr.length > 60) {
        majorHiddenDangersStr = majorHiddenDangersStr.slice(0, 60) + ' . . .';
      }
      $('#inspectionDetailsModal .tbodyList').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + content[i].numberOfFloor + '</td><td>' + content[i].hiddenDanger + '</td><td class="majorHiddenDangersBox"><span class="majorHiddenDangers majorHiddenDangersItemStyle none">'+content[i].majorHiddenDangers+'</span>' + majorHiddenDangersStr+ '</td><td class="'+active+'">' + level + '</td><td>' + duration + '</td><td><span class="seeDetailsStyle" id =' + content[i].id + '>查看详情</span></td></tr>');

    }
    // var level = content[0].level;
    // var active = '';
    // if (level == 0) {
    //   level = '重大';
    //   active = 'colorff4242';
    // } else if (level == 1) {
    //   level = '较大';
    //   active = 'colorffb142';
    // } else if (level == 2) {
    //   level = '一般';
    //   active = 'color61ff42';
    // } else if (level == 3) {
    //   level = '正常';
    //   active = 'color61ff42';
    // }
    // var reportingTime=content[0].reportingTime;
    // $('.headListLeft li .reportPerson').text(content[0].reportPerson);
    // $('.headListLeft li .inspectionSituation').text(content[0].inspectionSituation);
    // $('.headListLeft li .photoNumber').text(content[0].photoNumber);
    // $('.headListLeft li .reportingTime').text(reportingTime.substr(0,10));
    // $('.headListLeft li .numberOfFloor').text(content[0].numberOfFloor);
    // $('.headListLeft li .duration').text(content[0].duration);
    // $('.headListLeft li .buildingName').text(content[0].buildingName);
    // $('.headListLeft li .level').text(content[0].level);
    // $('.headListLeft li .communityName').text(content[0].communityName);
    // $('.headListLeft li .hiddenDanger').text(content[0].hiddenDanger);
    // $('.headListLeft li .address').text(content[0].address);
    // $('.headListLeft li .node').text(content[0].node);
    // $('.headListLeft li .organization').text(content[0].organization.orgName);
    patrollingPageFunc(inspectionDetailsPageNum, n);
  }).catch(function (err) {
    console.log(err);
    $('#inspectionDetailsModal .tbodyList').html('');
    $('#inspectionDetailsPageBox .pageJump').hide();
    // $('#inspectionDetailsModal .headListCenter').hide();

  });
  // 根据uuid查询楼宇信息---巡检详情上部分的信息
  var obj1 = {};
  obj1.uuid = uuid;
  obj1.onlyValue = onlyValue;
  // var ret = getBuildingsByUuid(obj1);
  var ret = getBuildingsByOnlyValue(obj1);
  ret.then(function (data) {
    console.log(data);
    var buildingPeople = data.data.buildingPeople;
    var building = data.data.building;
    var reportingTime = building.reportingTime;
    if (reportingTime) {
      reportingTime = moment(reportingTime).format('YYYY-MM-DD HH:mm');
    }else{
      reportingTime = '空';
    }
    var level = building.level;
    var active='';
    if (level == 0) {
      level = '重大';
      active = 'colorff4242';
    } else if (level == 1) {
      level = '较大';
      active = 'colorffb142';
    } else if (level == 2) {
      level = '一般';
      active = 'color61ff42';
    } else if (level == 3) {
      level = '正常';
      active = 'color61ff42';
    }
    $('.headListLeft li .level').addClass(active);
    $('.headListLeft li .reportPerson').text('');
    $('.headListLeft li .photoNumber').text('');
    if (buildingPeople[0]) {
      $('.headListLeft li .reportPerson').text(buildingPeople[0].name);
      $('.headListLeft li .photoNumber').text(buildingPeople[0].phoneNumber);
    }

    $('#inspectionDetailsModal .erweimaBtn').attr('buildingName', building.address);
    $('.headListLeft li .inspectionSituation').text(building.inspectionSituation);
    $('.headListLeft li .reportingTime').text(reportingTime);
    $('.headListLeft li .numberOfFloor').text(building.totalNumberOfFloors + '层');
    $('.headListLeft li .duration').text(building.duration);
    $('.headListLeft li .buildingName').text(building.buildingName);
    $('.headListLeft li .level').text(level);
    $('.headListLeft li .communityName').text(building.communityName);
    $('.headListLeft li .hiddenDanger').text(building.hiddenDanger);
    $('.headListLeft li .address').text(building.address);
    $('.headListLeft li .node').text(building.node);
    $('.headListLeft li .organization').text(building.organization.orgName);

    $('.headListLeft li .belongRegion').text(building.belongRegion);
    $('.headListLeft li .buildingCategory').text(buildingCategoryStrArr[building.buildingCategory]);
    $('.headListLeft li .orNotFireControlRoom').text(buildOrStrArr[building.orNotFireControlRoom]);
    $('.headListLeft li .unitCategory').text(unitCategoryStrArr[building.unitCategory]);
    $('.headListLeft li .orNotChargingStation').text(buildOrStrArr[building.orNotChargingStation]);
    $('.headListLeft li .orNotFireStation').text(buildOrStrArr[building.orNotFireStation]);
    if (building.orNotFireStation == 1) {
      $('.headListLeft li .orNotFireStation').css('color', '#8fff58');
    } else {
      $('.headListLeft li .orNotFireStation').css('color', '#e14247');
    }
    if (building.orNotChargingStation == 1) {
      $('.headListLeft li .orNotChargingStation').css('color', '#8fff58');
    } else {
      $('.headListLeft li .orNotChargingStation').css('color', '#e14247');
    }
    if (building.orNotFireControlRoom == 1) {
      $('.headListLeft li .orNotFireControlRoom').css('color', '#8fff58');
    } else {
      $('.headListLeft li .orNotFireControlRoom').css('color', '#e14247');
    }

    var photoUrl = building.photoUrl;
    $('#qrcodeInspectionDetails img').attr('src', photoUrl);
    if (photoUrl) {
      $('#qrcodeInspectionDetails img').show();
    } else {
      $('#qrcodeInspectionDetails img').hide();
    }

  }).catch(function (err) {
    console.log(err);
  });
}

function visualizationCanvasFunc() {
  $.ajax({
    // localhost:9092/building/getFloorHiddenDangerGraphic?buildingInspectionRecordId=1
    url: "/building/getFloorHiddenDangerGraphic?uuid=" + uuid,
    type: 'GET',
    async: false,
    success: function (res) {
      console.log(res.data);
      var data = res.data;
      if (res.status == 200) {
        $('#inspectionDetailsModal .headListCenter').show();
        var visualizationCanvas = echarts.init(document.getElementById("visualizationCanvas"));
        // var data1=[
        //   {value:10, name:'防火门管理'},
        //   {value:10, name:'安全出口及疏通通道\n的管理'},
        //   {value:10, name:'消防设施的维护保养'},
        //   {value:10, name:'电力系统的保养'},
        //   {value:10, name:'消防车道和消防登高\n场所的维护管理'},
        //   {value:10, name:'其他'},
        // ]
        var data1 = data;
        var data2 = [];
        var data3 = data1;
        var dataNum = 0;
        for (var i = 0; i < data3.length; i++) {
          dataNum += +data3[i].value;
        }
        for (var i = 0; i < data1.length; i++) {
          // data2.push(Percentage(data1[i].value, dataNum));
          data2.push(data1[i].name);
          // data3[i].name=Percentage(data1[i].value, dataNum);
        }
        console.log(dataNum, data2, data1, data3)
        $('#inspectionDetailsModal .headListCenter .totalHiddenTroublNumBox p').eq(0).text(dataNum);
        // app.title = '嵌套环形图';      
        var option = {
          color: ['#0096ff', '#8fff58', '#ff8b2e', '#ff3d3d', '#7e00ff', '#00d8ff'],
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
          },
          legend: {
            x: '65%',
            y: 'center',
            textStyle: {
              color: '#fff'
            },
            data: data2
          },
          series: [{
            name: '',
            type: 'pie',
            radius: ['40%', '50%'],
            label: {
              normal: {
                formatter: '{d}%  ',
                backgroundColor: 'RGBA(61, 84, 105, 0.9)',
                borderWidth: 1,
                borderRadius: 4,
                rich: {
                  a: {
                    color: '#999',
                    lineHeight: 22,
                    align: 'center'
                  },
                  b: {
                    fontSize: 16,
                    lineHeight: 33
                  },
                }
              }
            },
            center: ['30%', '50%'],
            data: data1
          }]
        };
        //   option = {
        //     color:['#0096ff','#8fff58','#ff8b2e','#ff3d3d','#7e00ff','#00d8ff'],
        //     title : {
        //         text: '',
        //         subtext: '',
        //         x:'left'
        //     },
        //     tooltip : {
        //         trigger: 'item',
        //         formatter: "{a} <br/>{b} : {c} ({d}%)"
        //     },
        //     legend: {
        //       x : '70%',
        //       y : 'center',
        //       textStyle: {color: '#fff'},
        //       data:data2
        //     },
        //     toolbox: {
        //         // show : true,
        //         feature : {
        //             // mark : {show: true},
        //             // dataView : {show: true, readOnly: false},
        //             magicType : {
        //                 // show: true,
        //                 type: ['pie', 'funnel']
        //             },
        //             // restore : {show: true},
        //             // saveAsImage : {show: true}
        //         }
        //     },
        //     // calculable : true,
        //     series : [
        //         {
        //             name:'',
        //             type:'pie',
        //             radius : [40, 70],
        //             center : ['35%', '50%'],
        //             roseType : 'area',
        //             data:data1
        //         }
        //     ]
        // };

        // 使用制定的配置项和数据显示图表
        visualizationCanvas.setOption(option);
      } else {
        $('#inspectionDetailsModal .headListCenter').hide();
      }
    },
    error: function (res) {
      console.log(res);
      $('#inspectionDetailsModal .headListCenter').hide();
    }
  })
}
// 点击地图详细信息按钮弹出详细信息页面
houseMasterSeeDetailsFunc1();

function houseMasterSeeDetailsFunc1() {
  $("body .houseMasterSeeDetails").on("click", function (event) {

    $('.inspectionDetails-pop').addClass('is-visible3');
    var $this = $(this);
    uuid = $this.attr('uuid');
    onlyValue = $this.attr('onlyValue');
    inspectionDetailsPageList(inspectionDetailsPageNumCurr, onlyValue, uuid);
    //创建二维码

    // createQRCode1('qrcodeInspectionDetails', 'https://iotss.zjzywl.com/weixin/miniprogram?uuid='+uuid, "img/logo.png");
    // createQRCode1('qrcodeInspectionDetails', uuid, "img/logo.png");
    var uuid1 = 1;
    if ($this.attr('uuid') != 'undefined') {
      uuid1 = $this.attr('uuid');
    }

    var photoUrl = $this.photoUrl;
    $('#qrcodeInspectionDetails img').attr('src', photoUrl);
    if (photoUrl) {
      $('#qrcodeInspectionDetails img').show();
    } else {
      $('#qrcodeInspectionDetails img').hide();
    }
    // console.log(buildingId1)
    $.ajax({
      // localhost:9092/building/getFloorHiddenDangerGraphic?buildingInspectionRecordId=1
      url: "/building/getFloorHiddenDangerGraphic?uuid=" + uuid1,
      type: 'GET',
      async: false,
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        if (res.status == 200) {
          var visualizationCanvas = echarts.init(document.getElementById("visualizationCanvas"));
          // var data1=[
          //   {value:10, name:'防火门管理'},
          //   {value:10, name:'安全出口及疏通通道\n的管理'},
          //   {value:10, name:'消防设施的维护保养'},
          //   {value:10, name:'电力系统的保养'},
          //   {value:10, name:'消防车道和消防登高\n场所的维护管理'},
          //   {value:10, name:'其他'},
          // ]
          var data1 = data;
          var data2 = [];
          var data3 = data1;
          var dataNum = 0;
          for (var i = 0; i < data3.length; i++) {
            dataNum += +data3[i].value;
          }
          for (var i = 0; i < data1.length; i++) {
            // data2.push(Percentage(data1[i].value, dataNum));
            data2.push(data1[i].name);
            // data3[i].name=Percentage(data1[i].value, dataNum);
          }
          $('#inspectionDetailsModal .headListCenter .totalHiddenTroublNumBox p').eq(0).text(dataNum);
          console.log(dataNum, data2, data1, data3)
          // app.title = '嵌套环形图';      
          option = {
            color: ['#0096ff', '#8fff58', '#ff8b2e', '#ff3d3d', '#7e00ff', '#00d8ff'],
            tooltip: {
              trigger: 'item',
              formatter: "{a} <br/>{b}: {c} ({d}%)",
              show:false
            },
            legend: {
              x: '65%',
              y: 'center',
              textStyle: {
                color: '#fff'
              },
              data: data2
            },
            series: [{
              name: '隐患汇总',
              type: 'pie',
              radius: ['40%', '50%'],
              label: {
                normal: {
                  formatter: '{d}%  ',
                  backgroundColor: 'RGBA(61, 84, 105, 0.9)',
                  borderWidth: 1,
                  borderRadius: 4,
                  rich: {
                    a: {
                      color: '#999',
                      lineHeight: 22,
                      align: 'center'
                    },
                    b: {
                      fontSize: 16,
                      lineHeight: 33
                    },
                  }
                }
              },
              center: ['30%', '50%'],
              data: data1
            }]
          };
          //   option = {
          //     color:['#0096ff','#8fff58','#ff8b2e','#ff3d3d','#7e00ff','#00d8ff'],
          //     title : {
          //         text: '',
          //         subtext: '',
          //         x:'left'
          //     },
          //     tooltip : {
          //         trigger: 'item',
          //         formatter: "{a} <br/>{b} : {c} ({d}%)"
          //     },
          //     legend: {
          //       x : '70%',
          //       y : 'center',
          //       textStyle: {color: '#fff'},
          //       data:data2
          //     },
          //     toolbox: {
          //         // show : true,
          //         feature : {
          //             // mark : {show: true},
          //             // dataView : {show: true, readOnly: false},
          //             magicType : {
          //                 // show: true,
          //                 type: ['pie', 'funnel']
          //             },
          //             // restore : {show: true},
          //             // saveAsImage : {show: true}
          //         }
          //     },
          //     // calculable : true,
          //     series : [
          //         {
          //             name:'',
          //             type:'pie',
          //             radius : [40, 70],
          //             center : ['35%', '50%'],
          //             roseType : 'area',
          //             data:data1
          //         }
          //     ]
          // };

          // 使用制定的配置项和数据显示图表
          visualizationCanvas.setOption(option);
        }
      },
      error: function (res) {
        console.log(res);
      }
    })
  });
}

function createQRCode1(id, url, src) {
  $('#' + id).empty();
  jQuery('#' + id).qrcode({
    render: 'canvas',
    text: url,
    background: "#fff", //二维码的后景色  
    foreground: "#000", //二维码的前景色  
    size: 170,
    // quiet: 1 // 安静区域 即周边空白区域
    src: src, //图片中央的二维码
  });

}